//
//  SettingsCell.m
//  SettingsPage
//
//  Created by CPX on 2018/6/13.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "SettingsCell.h"
#import "Masonry.h"
#import "YYCategories.h"

/**
 基类设置Cell
 @author yzl
 */
@interface SettingsCell()

//@property (nonatomic,strong) UILabel *titleLab;
@property (nonatomic,strong) UIImageView *accessoryImage;
@property (nonatomic,strong) UISwitch *switcher;
@property (nonatomic,strong) UIImageView *selectImageView;


@end

@implementation SettingsCell

+ (instancetype)cellWithTable:(UITableView *)table identifier:(NSString *)identifier{
    
    SettingsCell *cell = [table dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)setData:(SettingsModel *)data{
    _data = data;
    CGFloat offsetY = 0;
    if (data.subTitle) {
        offsetY = -10;
        [self.subLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(20);
            make.leading.mas_equalTo(16);
            make.trailing.mas_equalTo(-16);
            make.centerY.mas_equalTo(self.contentView).offset(-offsetY);
        }];
    }
    if (!_titleLab) {
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(20);
            make.leading.mas_equalTo(16);
            make.trailing.mas_equalTo(-16);
            make.centerY.mas_equalTo(self.contentView).offset(offsetY);
        }];
    }
    
    if ([data isMemberOfClass:[SettingsPushModel class]]) {
        self.accessoryImage.hidden = NO;
        if (data.accessoryText) {
            if (!_accessoryLab) {
                [self.accessoryLab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.trailing.mas_equalTo(self.accessoryImage.mas_trailing).offset(-15);
                    make.size.mas_equalTo(CGSizeMake(100, 20));
                    make.centerY.mas_equalTo(self.accessoryImage);
                }];
            }
            self.accessoryLab.text = data.accessoryText;
        }
    }
    else if ([data isMemberOfClass:[SettingsSwitchModel class]]){
        self.switcher.hidden = NO;
        self.switcher.on = data.switcherEnable;
    }
    else if ([data isMemberOfClass:[SettingsSelectModel class]]){
        self.selectImageView.hidden = !data.selected;
    }
    self.titleLab.text = data.title;
    if (data.subTitle) {
        self.subLab.text = data.subTitle;
    }
    if (data.accessoryText) {
        self.accessoryLab.text = data.accessoryText;
    }
}

- (void)setIsLast:(BOOL)isLast{
    _isLast = isLast;
    if (isLast) {
        self.line.hidden = YES;
    }
    else{
        self.line.hidden = NO;
    }
}

- (void)onSwitchTouched:(UISwitch*)sender{
    self.data.switcherEnable = sender.isOn;
    if (self.data.switchBlock) {
        self.data.switchBlock(sender.isOn);
    }
}

#pragma mark - lazy load

- (UILabel*)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.font = [UIFont systemFontOfSize:16];
        _titleLab.textColor = UIColorHex(001208);
        [self.contentView addSubview:_titleLab];
    }
    return _titleLab;
}

- (UILabel*)subLab{
    if (!_subLab) {
        _subLab = [UILabel new];
        _subLab.font = [UIFont systemFontOfSize:16];
        _subLab.textColor = UIColorHex(#334139CC);
        [self.contentView addSubview:_subLab];
    }
    return _subLab;
}

- (UIImageView*)accessoryImage{
    if (!_accessoryImage) {
        _accessoryImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next_ic"]];
        if (@available(iOS 9.0, *)) {
            if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
                _accessoryImage.image = [UIImage imageNamed:@"next_ic"].imageByFlipHorizontal;
            }
        } else {
            // Fallback on earlier versions
        }
        [self.contentView addSubview:_accessoryImage];
        [_accessoryImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(6.5, 11));
            make.trailing.mas_equalTo(-16);
            make.centerY.mas_equalTo(self.contentView);
        }];
    }
    return _accessoryImage;
}

- (UILabel*)accessoryLab{
    if (!_accessoryLab) {
        _accessoryLab = [UILabel new];
        _accessoryLab.font = [UIFont systemFontOfSize:15];
        _accessoryLab.textColor = UIColorHex(#334139CC);
        _accessoryLab.textAlignment = NSTextAlignmentRight;
        if (@available(iOS 9.0, *)) {
            if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
                _accessoryLab.textAlignment = NSTextAlignmentLeft;
            }
        } else {
            // Fallback on earlier versions
        }
        [self.contentView addSubview:_accessoryLab];
    }
    return _accessoryLab;
}

- (UISwitch*)switcher{
    if (!_switcher) {
        _switcher = [[UISwitch alloc] initWithFrame:CGRectZero];
        _switcher.onTintColor = UIColorHex(#0D7232);
        _switcher.transform = CGAffineTransformMakeScale(.8, .8);
        [_switcher addTarget:self action:@selector(onSwitchTouched:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:_switcher];
        if (@available(iOS 9.0, *)) {
            if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
                CGAffineTransform t = CGAffineTransformMakeRotation(-M_PI);
                _switcher.transform = CGAffineTransformScale(t, .8, -.8);
            }
        } else {
            // Fallback on earlier versions
        }
        [_switcher mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.mas_equalTo(-13);
            make.centerY.mas_equalTo(self.contentView);
        }];
    }
    return _switcher;
}

- (UIImageView*)selectImageView{
    if (!_selectImageView) {
        _selectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"select_ic_green"]];
        if (@available(iOS 9.0, *)) {
            if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
                _selectImageView.image = [UIImage imageNamed:@"select_ic_green"].imageByFlipHorizontal;
            }
        } else {
            // Fallback on earlier versions
        }
        [self.contentView addSubview:_selectImageView];
        [_selectImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(15, 15));
            make.centerY.mas_equalTo(self.contentView);
            make.trailing.mas_equalTo(-16);
        }];
    }
    return _selectImageView;
}

- (UIView*)line{
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = UIColorHex(#f4f4f4);
        [self.contentView addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.contentView).offset(-1);
            make.leading.mas_equalTo(16);
            make.trailing.mas_equalTo(-16);
            make.height.mas_equalTo(1);
        }];
    }
    return _line;
}

@end
